/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import gestionnaires.GestionnaireCommande;
import gestionnaires.GestionnaireUtilisateurs;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import modeles.Utilisateur;


@WebServlet(name = "ConnexionAdmin", urlPatterns = {"/admin/ConnexionAdmin"})
public class ConnexionAdmin extends HttpServlet {
  @EJB  
    private GestionnaireUtilisateurs gestionnaireUtilisateurs;
   @EJB
    private GestionnaireCommande gestionnaireCommandes; 
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String action = request.getParameter("action");
        HttpSession session = request.getSession(true);
        Utilisateur utilisateur =  null;
        
         
        if (action == null) {
            session.setAttribute("AdminIsConnected", false);
            RequestDispatcher dp = request.getRequestDispatcher("index.jsp");
            dp.forward(request, response);   
        }
         else if(action.equals("connexionAdmin")){
            String email_admin = request.getParameter("email");
            String password_admin = request.getParameter("password");
            if((utilisateur = gestionnaireUtilisateurs.getUserByEmail(email_admin)) == null){
                session.setAttribute("AdminIsConnected", false);
                request.setAttribute("messageConfirmation","<span class='error'>Aucun compte ne correspond à ces informations.</span>");
                RequestDispatcher dp = request.getRequestDispatcher("index.jsp");
                dp.forward(request, response);
            } else if(!utilisateur.getPassword().equals(password_admin)){
                session.setAttribute("AdminIsConnected", false);
                request.setAttribute("messageConfirmation","<span class='error'>Le mot de passe que vous avez entré n'est pas correct!</span>");
                RequestDispatcher dp = request.getRequestDispatcher("index.jsp");
                dp.forward(request, response);
            } else if(!utilisateur.isIsAdmin()) {
                session.setAttribute("AdminIsConnected", false);
                request.setAttribute("messageConfirmation","<span class='error'>Votre compte n'est pas enregistré comme administrateur, veuillez vous rendre sur cette <a href='../index.jsp' title='page d accueil' > page </a> pour réaliser des achats.</span>");
                RequestDispatcher dp = request.getRequestDispatcher("index.jsp");
                dp.forward(request, response);
            }
            else{
                session.setAttribute("AdminId", utilisateur.getId());
                session.setAttribute("Administrateur", utilisateur);
                session.setAttribute("AdminIsConnected", true);
                request.setAttribute("listeCommande",gestionnaireCommandes.getAllCommandes());
                request.setAttribute("nbCommandes",gestionnaireCommandes.getAllCommandes().size());
                RequestDispatcher dp = request.getRequestDispatcher("commandes.jsp");
                dp.forward(request, response);
            }
         }else if(action.equals("deconnexionAdmin")){
           session.setAttribute("AdminIsConnected", false);
           session.removeAttribute("AdminId");
           session.removeAttribute("Administrateur");
           RequestDispatcher dp = request.getRequestDispatcher("index.jsp");
           dp.forward(request, response);
         }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
